package com.example.myapplication0523.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.example.myapplication0523.R;
import com.example.myapplication0523.adapter.ViewPagerAdapter;
import com.example.myapplication0523.fragment.ToutiaoHomeFragment;
import com.example.myapplication0523.fragment.ToutiaoMallFragment;
import com.example.myapplication0523.fragment.ToutiaoMineFragment;
import com.example.myapplication0523.fragment.ToutiaoVideoFragment;
import com.example.myapplication0523.view.MyViewPager;

import java.util.ArrayList;
import java.util.List;

public class ToutiaoActivity extends FragmentActivity implements View.OnClickListener {
    private LinearLayout homeLay;
    private LinearLayout videoLay;
    private LinearLayout mallLay;
    private LinearLayout mineLay;
    private TextView homeTv;
    private TextView videoTv;
    private TextView mallTv;
    private TextView mineTv;
    private MyViewPager viewpager;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toutiao);
        initView(); //配置组件
        initData(); //配置数据
    }

    /**
     * 配置数据
     */
    private void initData() {
        List<Fragment> list= new ArrayList<>();
        list.add(new ToutiaoHomeFragment());
        list.add(new ToutiaoVideoFragment());
        list.add(new ToutiaoMallFragment());
        list.add(new ToutiaoMineFragment());
        ViewPagerAdapter adapter= new ViewPagerAdapter(getSupportFragmentManager(), list);
        viewpager.setAdapter(adapter);
    }

    /**
     * 配置组件
     */
    private void initView() {
        homeLay= findViewById(R.id.layout_home);
        videoLay= findViewById(R.id.layout_video);
        mallLay= findViewById(R.id.layout_mall);
        mineLay= findViewById(R.id.layout_mine);
        homeTv= findViewById(R.id.tv_home);
        videoTv= findViewById(R.id.tv_video);
        mallTv= findViewById(R.id.tv_mall);
        mineTv= findViewById(R.id.tv_mine);
        viewpager= findViewById(R.id.viewpager);

        homeLay.setOnClickListener(this);
        videoLay.setOnClickListener(this);
        mallLay.setOnClickListener(this);
        mineLay.setOnClickListener(this);

        homeLay.setSelected(true);
        homeTv.setTextColor(Color.RED);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.layout_home:
                homeLay.setSelected(true);
                videoLay.setSelected(false);
                mallLay.setSelected(false);
                mineLay.setSelected(false);
                homeTv.setTextColor(Color.RED);
                videoTv.setTextColor(Color.parseColor("#333333"));
                mallTv.setTextColor(Color.parseColor("#333333"));
                mineTv.setTextColor(Color.parseColor("#333333"));
                viewpager.setCurrentItem(0);
                break;
            case R.id.layout_video:
                homeLay.setSelected(false);
                videoLay.setSelected(true);
                mallLay.setSelected(false);
                mineLay.setSelected(false);
                homeTv.setTextColor(Color.parseColor("#333333"));
                videoTv.setTextColor(Color.RED);
                mallTv.setTextColor(Color.parseColor("#333333"));
                mineTv.setTextColor(Color.parseColor("#333333"));
                viewpager.setCurrentItem(1);
                break;
            case R.id.layout_mall:
                homeLay.setSelected(false);
                videoLay.setSelected(false);
                mallLay.setSelected(true);
                mineLay.setSelected(false);
                homeTv.setTextColor(Color.parseColor("#333333"));
                videoTv.setTextColor(Color.parseColor("#333333"));
                mallTv.setTextColor(Color.RED);
                mineTv.setTextColor(Color.parseColor("#333333"));
                viewpager.setCurrentItem(2);
                break;
            case R.id.layout_mine:
                homeLay.setSelected(false);
                videoLay.setSelected(false);
                mallLay.setSelected(false);
                mineLay.setSelected(true);
                homeTv.setTextColor(Color.parseColor("#333333"));
                videoTv.setTextColor(Color.parseColor("#333333"));
                mallTv.setTextColor(Color.parseColor("#333333"));
                mineTv.setTextColor(Color.RED);
                viewpager.setCurrentItem(3);
                break;
        }
    }
}
